/**
 * 
 */
package com.aat.dxfy.thread.demo3;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @date 2016年6月29日 下午12:02:58
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 功能是：
 * @info java spring
 * @update none
 */
public class Test1 {

	static int i1 = 0;
	static int i2=0;
	public static void main(String[] args) throws Exception {
		test01();
		test02();
		test03();
	}

	// 单线程测试
	public static void test01() {
		long l1 = System.currentTimeMillis();
		for (int i = 0; i < 5; i++) {
			int i1 = Fun.jisuan1();
			int i2 = Fun.jisuan2();
			int i3 = i1 + i2;
			System.out.println("test1  res  "+i3);
		}
		long l2 = System.currentTimeMillis();
		System.out.println((l2 - l1) + " 毫秒");
	}
	
	public static void test02() throws InterruptedException{
		long l1 = System.currentTimeMillis();
		for (int i = 0; i < 5; i++) {
			//
			Thread t1 = new Thread(new Runnable(){
				public void run() {
					i1=Fun.jisuan1();
				}
			});
			
			Thread t2 = new Thread(new Runnable(){
				public void run() {
					i2=Fun.jisuan1();
				}
			});
			t1.start();
			t2.start();
			t1.join();
			t2.join();
			int i3 = i1 + i2;
			System.out.println("test2  res  "+i3);
			//
		}
		long l2 = System.currentTimeMillis();
		System.out.println((l2 - l1) + " 毫秒");
	}
	
	public static void test03() throws Exception{
		long l1 = System.currentTimeMillis();
		for (int i = 0; i < 5; i++) {
			//
			Callable<Integer> call=new Callable<Integer>() {
				public Integer call() throws Exception {
					return Fun.jisuan1();
				}
			};
			FutureTask<Integer> task=new FutureTask<Integer>(call);
			Thread t1 =new Thread(task);
			t1.start();
			
			Callable<Integer> call2=new Callable<Integer>() {
				public Integer call() throws Exception {
					return Fun.jisuan1();
				}
			};
			FutureTask<Integer> task2=new FutureTask<Integer>(call2);
			Thread t2 =new Thread(task2);
			t2.start();
			t1.join();
			t2.join();
			i1 =task.get();
			i2 =task2.get();
			int i3 = i1 + i2;
			System.out.println("test3  res  "+i3);
			//
		}
		long l2 = System.currentTimeMillis();
		System.out.println((l2 - l1) + " 毫秒");
	}

	//
}
